package com.itsc.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.itsc.entities.Dictionary;
import com.itsc.service.inter.IDictionaryService;
import com.itsc.util.HttpUtil;
import com.itsc.util.page.Page;
import com.itsc.util.page.PageUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class DictionaryAction implements ModelDriven<Dictionary>{

	private Dictionary model = new Dictionary();
	private IDictionaryService dictionaryService;
	
	private List<Integer> check_id;
	
	private String searchname;// 查询条件
	/* 分页显示数据 */
	private int curPage = 1;// 当前页
	private int pageSize = 2;// 每页显示条数
	private int countPage;// 总页数
	
	public List<Integer> getCheck_id() {
		return check_id;
	}
	public void setCheck_id(List<Integer> check_id) {
		this.check_id = check_id;
	}
	public String getSearchname() {
		return searchname;
	}
	public void setSearchname(String searchname) {
		this.searchname = searchname;
	}
	public int getCurPage() {
		return curPage;
	}
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getCountPage() {
		return countPage;
	}
	public void setCountPage(int countPage) {
		this.countPage = countPage;
	}
	public void setDictionaryService(IDictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}
	@Override
	public Dictionary getModel() {
		// TODO Auto-generated method stub
		return model;
	}

	public String list() throws Exception{
		int count = dictionaryService.getCount();
		PageUtils.initParamter(count, pageSize);
		Page page = PageUtils.getPage();
		List<Dictionary> dictionaryList = dictionaryService.getPage(page);
		ActionContext.getContext().put("dictionaryList", dictionaryList);
		ActionContext.getContext().put("page", page);
		return "list";
	}
	
	public String addModel() throws Exception{
		
		return "insert";
	}
	
	public String add() throws Exception{
		dictionaryService.save(model);
		return "ok";
	}
	
	public String edit() throws Exception{
		
		Dictionary dictionary = dictionaryService.selectById(model.getDic_id());
		ActionContext.getContext().getValueStack().push(dictionary);
		return "edit";
	}
	
	public String update() throws Exception{
		dictionaryService.update(model);
		return "ok";
	}
	
	public String delete() throws Exception {
		dictionaryService.delete(model);
		return "ok";
	}
	
	public String moredelete() throws Exception{
		for(Integer id : check_id){
			Dictionary dictionary = dictionaryService.selectById(id);
			dictionaryService.delete(dictionary);
		}
		
		return "ok";
	}
	
	public String search() throws Exception {
		List<Dictionary> list = null;
		Page page = null;
		if (searchname == null && searchname.equals("")) {
			int count = dictionaryService.getCount();
			PageUtils.initParamter(count, pageSize);
			page = PageUtils.getPage();
			if (curPage <= 0) {
				curPage = 1;
			} else if (curPage > page.getTotalPage()) {
				curPage = page.getTotalPage();
			} else {
			}
			PageUtils.setCurrentPage(curPage);
			page = PageUtils.getPage();
			list = dictionaryService.search(searchname, page);
		} else {
			int count = dictionaryService.getCount(searchname);
			PageUtils.initParamter(count, pageSize);
			PageUtils.setCurrentPage(curPage);
			page = PageUtils.getPage();
			list = dictionaryService.search(searchname, page);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("page", page);
		String js = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);
		HttpUtil.rendText(js);
		return null;
	}
}
